package common.fengkai.filepost.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.sealcenter.webservice.impl.PdfIdActionService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 测试电子签章接口
 */
public class SignPDF {

    public static byte[]  sign_SM(byte[] file,JSONObject jsonObject) throws Exception {
        // String url = "http://10.0.100.57:7001/iSignatureWebPDF/services/PdfActionService";

        String url = "http://10.29.31.124:8002/eseal/iSignatureWebPDF/services/PdfActionService";
        // String url = "http://30.79.111.86/eseal/iSignatureWebPDF/services/PdfActionService";
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setServiceClass(PdfIdActionService.class);
        factoryBean.setAddress(url);
        PdfIdActionService pdfActionService = (PdfIdActionService) factoryBean.create();

        Client proxy = ClientProxy.getClient(pdfActionService);
        HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
        conduit.getClient().setReceiveTimeout(0L);
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(60000L);
        policy.setAllowChunking(false);
        policy.setReceiveTimeout(120000L);
        conduit.setClient(policy);
        String jsonString = jsonObject.toJSONString();
        System.out.println("jsonString==="+jsonString);
        if (!Objects.equals("com.ctc.wstx.stax.WstxInputFactory", System.getProperty("javax.xml.stream.XMLInputFactory"))) {
            System.setProperty("javax.xml.stream.XMLInputFactory", "com.ctc.wstx.stax.WstxInputFactory");
            System.setProperty("javax.xml.stream.XMLOutputFactory", "com.ctc.wstx.stax.WstxOutputFactory");
            System.setProperty("javax.xml.stream.XMLEventFactory", "com.ctc.wstx.stax.evt.WstxEventFactory");
        }

        byte[] result = pdfActionService.execByteToByte(file, jsonString);
        System.out.println("result.length:"+result.length);
        //太平财产保险有限公司青岛分公司中英文刘洪涛授权签名章
        //太平财产保险有限公司青岛分公司中英文刘洪涛授权签名章
        Files.write(Paths.get("C:\\Users\\carry\\Desktop\\项目资料\\66801060620200000006pdfsigned.pdf"), result);
        //Files.write(Paths.get("D:/TestSignd签名.pdf"), result);
        return  result;
    }


       public static void main(String[] args){


          }







}